if !exists(param.A)
  M291 S1 T0 R"Use Auto Calibration macro" P"Using separate calibration macros may cause issues.<br>Please use the Auto Calibration macro"
  abort "Error: Auto Calibration macro required."



;====Probe with Nozzle====

G90
G1 F18000 X0 Y80 Z5 F18000

G91
M98 P"0:/sys/nozzleprobe.g" Z1
var zn = move.axes[2].machinePosition
G1 Z20






;====Probe with Z - Probe====
M98 R1 P"0:/sys/attachedcheck.g" ; make sure probe is conected, pick if negative and leave relay active
M558 K0 P8 C"1.io4.in" H5 F100 T18000              ; define Z probe parameters
M98 P"0:/user/probeoffset.g"                       ; load global variables

G90                 ; absolute positioning
G1 X{0 - sensors.probes[0].offsets[0]} Y{80 - sensors.probes[0].offsets[1]} F18000

G91
G38.2 K0 Z0 ; Probe Z
G4 P260
var zp1 = move.axes[2].machinePosition
G1 Z5

G38.2 K0 Z0 ; Probe Z
G4 P260
var zp2 = move.axes[2].machinePosition
G1 Z5

G38.2 K0 Z0 ; Probe Z
G4 P260
var zp3 = move.axes[2].machinePosition
G1 Z5


var zp =  (var.zp1 + var.zp2 + var.zp3)/3


var dd = var.zp - var.zn

echo "New Z offset is: "^{var.dd}^" mm"


;======= Aditional offset ======= Increase (+) to lower, Decrease (-) to raise

set global.zoffset = var.dd - {0.0}  ; Apply babystepping to zoffset (add for less Z offset)

;======= Aditional offset =======

echo >"0:/user/probeoffset.g" "; Set Z probe trigger value, offset and trigger height"
echo >>"0:/user/probeoffset.g" "G31 P500 X0 Y28.50 Z"^{global.zoffset}
M98 P"0:/user/probeoffset.g"                       ; load global variables

echo >"0:/user/zoffset.g" "if exists(global.zoffset)"
echo >>"0:/user/zoffset.g" "  set global.zoffset = "^{global.zoffset}
echo >>"0:/user/zoffset.g" "else"
echo >>"0:/user/zoffset.g" "  global zoffset = "^{global.zoffset}

G30           ; Home bed using probe to update datum